package com.jason.www;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jason.www.pojo.Movie;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

/**
 * 创建TransportClient  和  基础的crud操作
 *官方建议 ：https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.4/client.html
         我们计划弃用TransportClient  ,Elasticsearch 7.0并在8.0中完全删除它。
         相反，您应该使用 Java高级REST客户端，它执行HTTP请求而不是序列化的Java请求
 */
public class ElasticsearchTest1 {

    private static final String IP_ADDRESS = "127.0.0.1";
    private static final String CLUSTER_NAME = "my-application";
    private static final Integer TCP_PORT = 9300;

    private static TransportClient client;

    /**
     * 通过TransportClient进行连接es服务端 对应端口9300(TCP端口9300，HTTP端口9200)
     * @throws UnknownHostException
     */
    @Before
    public void initES() throws UnknownHostException {
        //这里需要制定集群名，不然会报错NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{3QGslRCASXmIZLb6SAugEA}{192.168.6.112}{192.168.6.112:9300}]
        Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();

       client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(IP_ADDRESS), TCP_PORT));
        System.out.println(client.nodeName());


    }
    @After
    public void shutdownES(){
        client.close();
    }


    /**
     * GET localhost:9200/_search
     * GET localhost:9200/movies/_search
     * GET localhost:9200/movies/movie/_search
     */
    @Test
    public void searchES(){
        ActionFuture<SearchResponse> actionFuture = client.search(new SearchRequest());//localhost:9200/_search
        System.out.println(actionFuture.actionGet());
        System.out.println(client.search(new SearchRequest("movies")).actionGet());
        System.out.println(client.search(new SearchRequest("movies").types("movie")).actionGet());

    }

    /**
     * GET localhost:9200/movies/movie/1
     */
    @Test
    public void getIndexTest(){
        ActionFuture<GetResponse> actionFuture = client.get(new GetRequest("movies", "movie", "1"));
        GetResponse resp = actionFuture.actionGet();
        System.out.println(resp);
        Movie movie = JSONObject.parseObject(resp.getSourceAsString(), Movie.class);
        System.out.println(movie);
    }

    /**
     * PUT /movies/movie/1
     * {
     *     "title": "The Godfather",
     *     "director": "Francis Ford Coppola",
     *     "year": 1972
     * }
     */
    @Test
    public void setIndexTest() {
        /*IndexResponse resp = client.prepareIndex("movies", "movie", "1").setSource(
                XContentFactory.jsonBuilder().startObject()
                        .field("title", "Kill Bill: Vol. 1")
                        .field("director", "Quentin Tarantino")
                        .field("year", "2003")
                        .field("genres", new String[]{"Action", "Crime", "Thriller"})
                        .endObject()).get();*/

        Movie movie = new Movie("Kill Bill: Vol. 1","Quentin Tarantino",2018, Arrays.asList("Action", "Crime", "Thriller"));
        IndexResponse resp = client.prepareIndex("movies", "movie", "1").setSource(JSON.toJSONString(movie), XContentType.JSON).get();
        System.out.println(resp.toString());
    }

    /**
     * DELETE localhost:9200/movies/movie/1
     */
    @Test
    public void delIndex(){
        DeleteResponse deleteResponse = client.delete(new DeleteRequest("movies", "movie", "1")).actionGet();
        System.out.println(deleteResponse.status());
        getIndexTest();
    }

}
